﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Modules.ACTP.Components;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Security;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class ProjectList : ModulePage
    {
        protected string GetProjectManagerAsHtml(object o)
        {
            int projectID = Convert.ToInt32(o);
            Employee employee = Employee.GetEmployeeBy_ProjectID_ProjectManager(this.ModuleID, projectID);
            if (employee != null)
                return employee.EmployeeCode;
            return string.Empty;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            gvList.Columns[0].Visible = this.IsEditable;
            gvList.Columns[1].Visible = this.IsDeleteable;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Team.
                if (PortalSecurity.HasManageAuthorization(this.ModuleID) || PortalSecurity.IsInRoles("1"))
                {
                    ddlTeam.DataSource = Team.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                    ddlTeam.DataBind();
                }
                else
                {
                    try
                    {
                        int employeeID = Employee.GetEmployeeBy_MembershipID(PortalSecurity.CurrentUser.Identity.ID).EmployeeID;
                        ddlTeam.DataSource = Team.SelectCollectionBy_EmployeeID(this.ModuleID, employeeID).OrderBy(x => x.Title);
                        ddlTeam.DataBind();
                    }
                    catch (Exception)
                    {
                        
                    }
                    
                }
                // Site admin / Manager
                ProjectDataBinding();
            }
        }

        private void ProjectDataBinding()
        {
            if (PortalSecurity.HasManageAuthorization(this.ModuleID) || PortalSecurity.IsInRoles("1"))
            {
                if (this.ItemID > 0)
                {
                    gvList.DataSource = Project.SelectCollectionBy_TeamID(this.ItemID);
                    gvList.DataBind();
                }
                else
                {
                    gvList.DataSource = Project.SelectCollectionAll(this.ModuleID);
                    gvList.DataBind();
                }
            }
            else
            {
                if (this.ItemID > 0)
                {
                    int employeeID = Employee.GetEmployeeBy_MembershipID(PortalSecurity.CurrentUser.Identity.ID).EmployeeID;
                    gvList.DataSource = Project.SelectCollectionBy_EmployeeID(this.ModuleID, employeeID).Where(x=>x.TeamID == this.ItemID);
                    gvList.DataBind();
                }
                else
                {
                    int employeeID = Employee.GetEmployeeBy_MembershipID(PortalSecurity.CurrentUser.Identity.ID).EmployeeID;
                    gvList.DataSource = Project.SelectCollectionBy_EmployeeID(this.ModuleID, employeeID);
                    gvList.DataBind();
                }
                    
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            gvList.DataSource = Project.SelectCollectionAll(this.ModuleID);
            gvList.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int teamID = Convert.ToInt32(ddlTeam.SelectedValue);
            if (teamID == 0)
            {
                gvList.DataSource = Project.SelectCollectionAll(this.ModuleID);
                gvList.DataBind();
            }
            else
            {
                gvList.DataSource = Project.SelectCollectionBy_TeamID(teamID);
                gvList.DataBind();
            }
        }

        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataKey key = gvList.DataKeys[e.RowIndex];
            if (key != null)
            {
                int projectID = Convert.ToInt32(key.Value);
                IList<ProjectTask> projectTasks = ProjectTask.SelectCollectionBy_ProjectID(projectID);
                ProjectTask.DeleteBy_ProjectID(projectID);
                foreach (ProjectTask projectTask in projectTasks)
                {
                    Task.DeleteTask(projectTask.TaskID);
                }
                
                TaskMilestone.DeleteBy_ProjectID(projectID);
                Project.DeleteProject(projectID);
                this.ProjectDataBinding();
            }
        }
    }
}